#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void genArrRand(int, float *);
void printArr(int, float *);
void getStatistics(int, float *, float *, float *, float *, float *);

int main()
{
    int n;

    printf("input the length of 1D array: ");

    scanf("%d", &n);

    float arr[n];
    float mean, var, maxV, minV;

    genArrRand(n, arr);

    printArr(n, arr);

    getStatistics(n, arr, &mean, &var, &maxV, &minV);

    printf("mean = %.3f, variance = %.3f, max = %.3f, min = %.3f\n", mean, var, maxV, minV);

    return 0;
}

void genArrRand(int n, float *arr)
{
    srand(time(NULL));

    for (int i = 0; i < n; i ++)
    {
        arr[i] = (float)rand() / RAND_MAX;
    }
}

void printArr(int n, float *arr)
{
    for (int j = 0; j < n; j ++)
    {
        printf("%.3f ", arr[j]);
    }

    printf("\n");
}


void getStatistics(int n, float *arr, float *mean, float *var, float *maxV, float *minV)
{
    float sum = 0;
    int max_v, min_v;
    float sigma;

    max_v = 0;
    min_v = 0;

    for (int k = 0; k < n; k ++)
    {
        sum += arr[k];

        if ((arr[k] - arr[max_v]) > 1e-10)
        {
           max_v = k;
        }
        if ((arr[min_v] - arr[k]) > 1e-10)
        {
           min_v = k;
        }
    }

    *mean = sum / n;

    for (int s = 0; s < n; s ++)
    {
        sigma += (arr[s] - *mean) * (arr[s] - *mean);
    }

    *maxV = arr[max_v];
    *minV = arr[min_v];
    *var = sigma / n;
}
